// store.ts
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref()
    const setToken = (newToken: string) => {
      token.value = newToken
    }
    const removeToken = () => {
      token.value = null
    }
    const userInfo = ref()
    const setUserInfo = (newUserInfo: any) => {
      userInfo.value = newUserInfo
    }
    const removeInfo = () => {
      userInfo.value = null
    }
    return {
      token,
      setToken,
      removeToken,
      userInfo,
      setUserInfo,
      removeInfo
    }
  },
  {
    persist: {
      key: 'user', // 在缓存里面 key 值
      storage: localStorage,
      paths: ['token','userInfo'], // 需要缓存哪些变量
    },
  }
)

// 在页面上使用
// import { useTestStore } from "./store";

// testPinia() {
//   const testStore = useTestStore();
//   // 查看Storage应该存在了 localStorage
//   testStore.testCache = `测试缓存${this.model.test.value}`;
// }
